package com.mall.order.services;

import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.handler.InitOrderHandler;
import com.mall.order.biz.handler.LogisticalHandler;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.CartProductDto;
import com.mall.order.dto.CreateSeckillOrderRequest;
import com.mall.order.dto.CreateSeckillOrderResponse;
import com.mall.order.OrderPromoService;
import com.mall.shopping.IProductService;
import com.mall.shopping.dto.ProductDetailDto;
import com.mall.shopping.dto.ProductDetailRequest;
import com.mall.shopping.dto.ProductDetailResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lichao
 * @createdate 2021-02-02 10:47
 */
public class OrderPromoServiceImpl implements OrderPromoService {

    @Autowired
    InitOrderHandler initOrderHandler;
    @Autowired
    LogisticalHandler logisticalHandler;
    @Reference(interfaceClass = IProductService.class,check = false)
    IProductService iProductService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CreateSeckillOrderResponse createSeckillOrder(CreateSeckillOrderRequest request) {

        ProductDetailResponse productDetail = iProductService.getProductDetail(new ProductDetailRequest(request.getProductId()));
        ProductDetailDto productDetailDto = productDetail.getProductDetailDto();
        CartProductDto cartProductDto = new CartProductDto();
        cartProductDto.setProductId(Long.valueOf(request.getProductId()));
        cartProductDto.setSalePrice(request.getPrice());
        cartProductDto.setProductNum(1L);
        cartProductDto.setLimitNum(1L);
        cartProductDto.setChecked("false");
        cartProductDto.setProductName(productDetailDto.getProductName());
        cartProductDto.setProductImg(productDetailDto.getProductImageBig());
        List<CartProductDto> cartProductDtos = new ArrayList<>();
        cartProductDtos.add(cartProductDto);


        CreateOrderContext createOrderContext = new CreateOrderContext();
        createOrderContext.setUserId(request.getUserId());
        createOrderContext.setOrderTotal(request.getPrice());
        createOrderContext.setCartProductDtoList(cartProductDtos);
        createOrderContext.setBuyerNickName(request.getUsername());


        boolean ret = initOrderHandler.handle(createOrderContext);

        createOrderContext.setStreetName(request.getStreetName());
        createOrderContext.setTel(request.getTel());
        logisticalHandler.handle(createOrderContext);

        CreateSeckillOrderResponse response = new CreateSeckillOrderResponse();

        if(ret){
            response.setRetCode(OrderRetCode.SUCCESS);
            response.setMsg("success");
        }else {
            response.setRetCode(OrderRetCode.SYSTEM_ERROR);
            response.setMsg("fail");
        }

        return response;
    }
}
